//
//  MytableBatController.m
//  8-24 TableBarController
//
//  Created by mac on 15-8-24.
//  Copyright (c) 2015年 徐鹏. All rights reserved.
//

#import "MytableBatController.h"

@interface MytableBatController (){

    UIView *_contentView;
    UIView *_tableBar;
    //记录当前选中的button
    UIButton *select_btn;

}

@end

@implementation MytableBatController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIViewController *VC1=[[UIViewController alloc]init];
    VC1.view.backgroundColor=[UIColor redColor];
    UIViewController *VC2=[[UIViewController alloc]init];
    VC2.view.backgroundColor=[UIColor orangeColor];
    UIViewController *VC3=[[UIViewController alloc]init];
    VC3.view.backgroundColor=[UIColor greenColor];
    self.viewControll_array=[NSArray arrayWithObjects:VC1,VC2,VC3, nil];
    _contentView=[[UIView alloc]initWithFrame:self.view.frame];
    _contentView.backgroundColor=[UIColor blueColor];
    [self.view addSubview:_contentView];
    _tableBar=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height-49, 320, 49)];
    _tableBar.backgroundColor=[UIColor yellowColor];
    [self.view addSubview:_tableBar];
    int count =[_viewControll_array count];
    CGFloat width=320/count;
    NSArray *normalImages=@[@"1.jpg",@"4.jpg",@"5.jpg"];
    NSArray *selectedImages=@[@"2.jpg",@"3.jpg",@"6.jpg"];
    for (int i=0; i<count ; i ++) {
        UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
        button.frame=CGRectMake(width *i  , 0, 320, width);
        [button setBackgroundImage:[UIImage imageNamed:normalImages[i]] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:selectedImages[i]] forState:UIControlStateSelected];
        button.tag= i ;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
         [_tableBar addSubview:button];
        if (i==0) {
            [self buttonClick:button];
        }
    
    }
}
-(void)buttonClick:(UIButton *)btn
{
    //吧上一个按钮恢复到普通状态
    select_btn.selected=NO;
    //选中的button的状态改为选中的状态
    btn.selected=YES;
    //记录当前选中的button
    select_btn=btn;
    //切换对应的视图;
    UIViewController *VC=self.viewControll_array[btn.tag];
    //判断vc的View 是否已经删除
    if ([_contentView.subviews containsObject:VC.view]) {
        [_contentView bringSubviewToFront:VC.view];
    }else{
    [_contentView addSubview:VC.view];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
